package com.kadron.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import com.kadron.entity.ItemColor;

@AutoCreate
@BypassInterceptors
@Name("itemColorDAO")
public class KItemColorDAO extends KDAO<ItemColor> {

	private static final Log log = LogFactory.getLog(KItemColorDAO.class);
	
	public ItemColor find(long itemId, long colorId) {
		EntityManager em = getEntityManager();
		try {			
			return (ItemColor) em.createQuery("SELECT e FROM ItemColor e WHERE e.item.id = :it_id AND e.color.id = :color_id ")
			.setParameter("it_id", itemId)
			.setParameter("color_id", colorId)
			.getSingleResult();
		} catch (NoResultException e) {
			log.debug("ItemColor not found");
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<ItemColor> getItemColor(long itemId) {
		EntityManager em = getEntityManager();
		return em.createQuery("SELECT p FROM ItemColor p WHERE " +
				"p.item.id = :it_id ")
		.setParameter("it_id", itemId)
		.getResultList();
	}
	
}
